<?php
/**
 * Results
 *
 * Supports DBConn(), a basic database abstraction class. 
 * It manages result elements such as fields, values, and iteration.
 *
 *@author Chris Wetherell
 *@version 1.0
 *@copyright none
 */  
class Results {
  var $results;
  var $dbtype;
  var $row;
  
  function Results($dbtype) {
    $this->dbtype = $dbtype; 
  }
  
  function setResults($resultsFromQuery) {
    $this->results = $resultsFromQuery; 
  }
  
  function getResults() {
    return $this->results;
  }
  
  function getRowCount() {
     if($this->dbtype == $GLOBALS["DB_TYPE_ID_MYSQL"]) 
     {
       return mysql_num_rows($this->results);
     }
     if($this->dbtype == $GLOBALS["DB_TYPE_ID_ACCESS"]) 
     {
     // TODO: Support row counting for MS ACCESS
     }
     if($this->dbtype == $GLOBALS["DB_TYPE_ID_ORACLE"]) 
     {
     // TODO: Support row counting for Oracle DBs
     }
  }
  
  function nextRow() {
     if($this->dbtype == $GLOBALS["DB_TYPE_ID_ACCESS"]) 
     {
       $this->results->MoveNext();
     }
  }
  function getRow() {
     if($this->dbtype == $GLOBALS["DB_TYPE_ID_ACCESS"]) 
     {
       if ($this->results->EOF) { return false; } 
       $this->row = $this->results;
      return $this;
     }
     if($this->dbtype == $GLOBALS["DB_TYPE_ID_MYSQL"]) 
     {
       $this->row = mysql_fetch_array($this->results);
      if ($this->row==false) { return null;}
      return $this;
     }
     if($this->dbtype == $GLOBALS["DB_TYPE_ID_ORACLE"]) 
     {
       $this->row = ocifetch($this->results);
      if ($this->row==false) { return null;}
      return $this;
     }
  }
  
  function getFieldCount() {
     if($this->dbtype == $GLOBALS["DB_TYPE_ID_ACCESS"]) 
     {
      $field = $this->row->Fields(); 
       return $field->count();
     }
     if($this->dbtype == $GLOBALS["DB_TYPE_ID_MYSQL"]) 
     {
       return sizeof($this->row)/2;
     }
     if($this->dbtype == $GLOBALS["DB_TYPE_ID_ORACLE"]) 
     {
       return ocinumcols($this->getResults());
     }
  }
  
  function getFieldNameFromIndex($strIndex) {
     if($this->dbtype == $GLOBALS["DB_TYPE_ID_ACCESS"]) 
     {
      $field = $this->row->Fields($strIndex); 
       return $field->name;
     }
     if($this->dbtype == $GLOBALS["DB_TYPE_ID_MYSQL"]) 
     {
      return mysql_field_name($this->results,$strIndex);
     }
     if($this->dbtype == $GLOBALS["DB_TYPE_ID_ORACLE"]) 
     {
      return ocicolumnname($this->results,$strIndex+1);
     }
  }
  
  function getDateField($strFormat,$strName) {
     if($this->dbtype == $GLOBALS["DB_TYPE_ID_ACCESS"]) 
     {
      $field = $this->row->Fields($strName); 
      $date = $field->value; 
      if ($date>0) {      
        $date = date($strFormat,$date);
      }
       return $date;
     }
     if($this->dbtype == $GLOBALS["DB_TYPE_ID_MYSQL"]) 
     {
       $date = $this->row[$strName];
      if (gettype($date)=="string") {
        $date_time_string =  $date;
        
        // Split the string into 3 parts  - date, time and AM/PM
        $dt_elements =  explode(" " ,$date_time_string);
        
        // Split the date up
        $date_elements =  explode("-" ,$dt_elements[0]);
        
        // Split the time up
        $time_elements =  explode(":" ,$dt_elements[1]);
        
        // If we have a PM then we can  add 12 hours to the hour to get into 24 hour time.
        if (isset($dt_elements[2]) && strtoupper($dt_elements[2])=="PM") { 
            $time_elements[0]+=12;
        }
        
        // output the result
        $timestamp = mktime(
              $time_elements[0], 
              $time_elements[1], 
              $time_elements[2], 
              $date_elements[1], 
              $date_elements[2], 
              $date_elements[0]);
        if ($timestamp>0) {      
          $date = date($strFormat,$timestamp);
        } else { 
          return ""; 
        }
      }
       return $date;
     }
  }
  
  function getField($strName) {
     if($this->dbtype == $GLOBALS["DB_TYPE_ID_ACCESS"]) 
     {
      $field = $this->row->Fields($strName); 
       return $field->value;
     }
     if($this->dbtype == $GLOBALS["DB_TYPE_ID_MYSQL"]) 
     {
       return $this->row[$strName];
     }
     if($this->dbtype == $GLOBALS["DB_TYPE_ID_ORACLE"]) 
     {
      if (gettype($strName)=="integer") $strName++;
       return ociresult($this->results,$strName);
     }
     
  }

}